package stud.mpgame.flash.scene;

import flash.display.Sprite;

import stud.mpgame.flash.game.Player;

import stud.mpgame.flash.game.GameWorld;

import stud.mpgame.flash.game.GameUtils;
import stud.mpgame.flash.scene.LoadingScene;
import stud.mpgame.flash.map.JsonDataProvider;
import stud.mpgame.flash.map.hxjson2.JSON;
import stud.mpgame.flash.map.GameMap;


import flash.geom.Point;
import flash.Lib;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.DisplayObject;
import stud.mpgame.flash.scene.GameWorldView;

import stud.mpgame.flash.assets.ImageManager; 

/**
 * ...
 * @author pavel[f.pavel91@gmail.com]
 */
class GameWorldView extends Sprite {
	
    private var playerViewList:List< PlayerView >;
	
	private static var URL:String;
	private static var hIm :Float;
	private static var wIm :Float;
	 
	private static var startPositionX = 0;
	private static var startPositionY = 2;
	 
	private static var mapHeight : Int;
	private static var mapWidth : Int;
	 
	private static var massiv:Array<Int>;
	private static var mapView:Array<Int>;
	private static var mapModel:Array<Int>;
	 
	 
	private var scene:LoadingScene;
	 //private static var player : DisplayObject;
	private  var myID:Int;
	 
	//private static var x:Float = 400;
	//private static var y:Float = 400;
	 
	private var gameMap:GameMap;
	 
    public function new(f:LoadingScene, ID:Int) {
        super();		
		this.scene = f;
		this.myID = ID;
    }

	public function initPlayers(gameWorld:GameWorld) {
		playerViewList = new List< PlayerView >();
		var serverPlayers:Array< Player > = gameWorld.getPlayers();
		for (serverPlayer in serverPlayers)
			addPlayer(serverPlayer);
	}
	
	
	public function updateWorld(gameWorld:GameWorld) {
        var serverPlayers:Array< Player > = gameWorld.getPlayers();
		if (serverPlayers.length > 0) {
			/*for (player in playerViewList) {
				if (player.getID() == this.myID) {
					x = player.getX() - x;
					y = player.getY() - y;
				}		
			}*/

			for (playerView in playerViewList) {
				var serverPlayer = GameUtils.getPlayerByID(serverPlayers, playerView.getID());
				//trace(playerView.x + "; " + playerView.y + " = " + serverPlayer.getX() + "; " + serverPlayer.getY());
				playerView.setCoords(serverPlayer.getX(), serverPlayer.getY());
			    
				//playerView.changeCoords(serverPlayer.getX() - playerView.getX(), serverPlayer.getY() - playerView.getY(), this.getMyId());
			}
        }
	}    
	
	public function getMyId():Int {
		return myID;	
	}
	 
    public function addPlayer(player:Player):Void {
        playerViewList.push(new PlayerView(player.getX(), player.getY(), player.getID(), this));        
    }
    
    public function removePlayer(player:Player):Void {
        for (playerView in playerViewList)
			if (playerView.getID() == player.getID()) {
				playerViewList.remove(playerView);
				return;
			}
    }
	
	//drawing all the scene
	public function draw(url:String) {
		URL = url;
		var js:JsonDataProvider = new JsonDataProvider();
		js.provide(URL, composeScene);
    }
	 
	public function getGameMap():GameMap {
		return gameMap;
	}
	  
	//composing scene 
	public function composeScene(gmap:GameMap):Void {
		 
		gameMap = gmap;
		if (gameMap == null)
			trace("BAD");
		
		var im = ImageManager.getInstance();
		loadMap(im, gmap);
				
		var bitmap = im.getBitmapByID(0);
		
		// painting map
		for (i in 0...gmap.map.view.length) {	
			if (mapView[i] == 0)
				continue;
			bitmap = im.getBitmapByID(mapView[i]);		
			bitmap.x = wIm * (i % mapWidth);
			bitmap.y = hIm * ((i- (i % mapWidth))/mapWidth);
			
			addChild(bitmap);
		}
	}	
	
	//loading different map's parametras
	public function loadMap(im:ImageManager, gmap:GameMap):Void {
		
		var bitmap = im.getBitmapByID(0);
		hIm = bitmap.height;
		wIm = bitmap.width;
		
		mapWidth = gmap.map.size.width;
		mapHeight = gmap.map.size.height;
		
		mapView = new Array<Int>();
		mapModel = new Array<Int>();
		
		for (i in 0...gmap.map.view.length) {
			//copying view
			mapView[i] = gmap.map.view[i];
			//building model
			if (mapView[i] == 0 )
				mapModel[i] = 0;
			else {
				if (mapView[i] == 5 || mapView[i] == 16 )
					mapModel[i] = 2;	
				else {
					if (mapView[i] == 6 || mapView[i] == 17 )
						mapModel[i] = 3;
					else {
						 if (mapView[i] == 19 || mapView[i] == 20)
							mapModel[i] = 4;
						 else {
							if (mapView[i] == 18 || mapView[i] == 21)
								mapModel[i] = 5;
							else
								mapModel[i] = 1;
						}	
					}
				}
			}
		}
		scene.mapLoadingFinished(); 
	}
	
	
}
	